package com.cos.shop.web.servlet;

import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.entity.User;
import com.cos.shop.entity.vo.CartVo;
import com.cos.shop.service.CartServices;
import com.cos.shop.service.impl.CartServicesImpl;
import com.cos.shop.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: CartController
 * Author: saber
 * Date:2022/9/19 9:18
 */
@WebServlet("/cart")
public class CartController extends BaseServlet {

    private CartServices cs = new CartServicesImpl();

    public String addCart(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.SystemConst.IS_LOGIN);

        int pid = StringUtils.stringToInt(request.getParameter("pid"));

        int uid = user.getUid();

        ResponseResult rr = cs.addCart(uid,pid);

        request.setAttribute("rr",rr);

        return Const.FORWARD+"/cartSuccess.jsp";
    }

    public String getCart(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.SystemConst.IS_LOGIN);

        ResponseResult rr = cs.getCartList(user.getUid());

        request.setAttribute("rr",rr);

        return Const.FORWARD+"/cart.jsp";
    }

    public void updateCartNum(HttpServletRequest request, HttpServletResponse response){

        User user = (User) request.getSession().getAttribute(Const.SystemConst.IS_LOGIN);

        int num = StringUtils.stringToInt(request.getParameter("num"));
        int pid = StringUtils.stringToInt(request.getParameter("pid"));

        ResponseResult rr = cs.updateCartNum(user.getUid(), pid, num);

    }

    public void clearCart(HttpServletRequest request, HttpServletResponse response){

        User user = (User) request.getSession().getAttribute(Const.SystemConst.IS_LOGIN);

        int pid = StringUtils.stringToInt(request.getParameter("pid"));

        ResponseResult rr = cs.clearCart(user.getUid(), pid);

    }

} 
